home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio / Ham Radio CD-ROM (Emerald Software) (1995).ISO / misc / 9q920411 / sockutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  1.9 KB  |  102 lines

  1. /* Low level socket routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "socket.h"
  8. #include "usock.h"
  9.  
  10. /* Convert a socket (address + port) to an ascii string of the form
  11.  * aaa.aaa.aaa.aaa:ppppp
  12.  */
  13. char *
  14. psocket(p)
  15. void *p;
  16. {
  17.     struct sockaddr *sp;    /* Pointer to structure to decode */
  18.  
  19.     sp = (struct sockaddr *)p;
  20.     if(sp->sa_family < AF_INET || sp->sa_family >= NAF)
  21.         return NULLCHAR;
  22.  
  23.     return (*Psock[sp->sa_family])(sp);
  24. }
  25.  
  26. /* Return ASCII string giving reason for connection closing */
  27. char *
  28. sockerr(s)
  29. int s;    /* Socket index */
  30. {
  31.     register struct usock *up;
  32.     struct socklink *sp;
  33.  
  34.     if((up = itop(s)) == NULLUSOCK){
  35.         errno = EBADF;
  36.         return Badsocket;
  37.     }
  38.     sp = up->sp;
  39.     if(sp->error != NULL){
  40.         return sp->error[up->errcodes[0]];
  41.     } else {
  42.         errno = EOPNOTSUPP;    /* not yet, anyway */
  43.         return NULLCHAR;
  44.     }
  45. }
  46. /* Get state of protocol. Valid only for connection-oriented sockets. */
  47. char *
  48. sockstate(s)
  49. int s;        /* Socket index */
  50. {
  51.     register struct usock *up;
  52.     struct socklink *sp;
  53.  
  54.     if((up = itop(s)) == NULLUSOCK){
  55.         errno = EBADF;
  56.         return NULLCHAR;
  57.     }
  58.     if(up->cb.p == NULLCHAR){
  59.         errno = ENOTCONN;
  60.         return NULLCHAR;
  61.     }
  62.     sp = up->sp;    
  63.     if(sp->state != NULL)
  64.         return (*sp->state)(up);
  65.     
  66.     /* Datagram sockets don't have state */
  67.     errno = EOPNOTSUPP;
  68.     return NULLCHAR;
  69. }
  70. /* Convert a socket index to an internal user socket structure pointer */
  71. struct usock *
  72. itop(s)
  73. register int s;    /* Socket index */
  74. {
  75.     register struct usock *up;
  76.  
  77.     s -= SOCKBASE;
  78.     if(s < 0 || s >= Nusock)
  79.         return NULLUSOCK;
  80.  
  81.     up = &Usock[s];
  82.  
  83.     if(up->type == NOTUSED)
  84.         return NULLUSOCK;
  85.     return up;
  86. }
  87.  
  88. void
  89. st_garbage(red)
  90. int red;
  91. {
  92.     int i;
  93.     struct usock *up;
  94.  
  95.     for(i=SOCKBASE;i<SOCKBASE+Nusock;i++){
  96.         up = &Usock[i];
  97.         if(up->type == TYPE_LOCAL_STREAM)
  98.             mbuf_crunch(&up->cb.local->q);
  99.     }
  100. }
  101.  
  102.